package com.sxkiler.demo.solr;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;
import com.sxkiler.demo.model.*;

@Component
public class SolrManager
{
    private  String solrUrl = "";
    private  String solrCore = "";

    @Value("${solr.url}")
    public void setSolrUrl(String solrUrl)
    {
        solrUrl = solrUrl;
    }

    @Value("${solr.core}")
    public void setSolrCore(String solrCore)
    {
        solrCore = solrCore;
    }

    public  String getSolrUrl()
    {
        return solrUrl;
    }

    public  void addDocs(List<Map<String, Object>> list)
            throws Exception
    {
        SolrInputDocument solrInput = null;
        Collection<SolrInputDocument> solrList = new ArrayList();
        for (Map<String, Object> map : list)
        {
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
            solrInput = new SolrInputDocument(new String[0]);
            while (it.hasNext())
            {
                Map.Entry<String, Object> entry = (Map.Entry)it.next();
                solrInput.addField((String)entry.getKey(), entry.getValue());
            }
            solrList.add(solrInput);
        }
        if (solrList.size() > 0)
        {
            Object client = new HttpSolrClient.Builder(solrUrl).build();
            ((SolrClient)client).add(solrCore, solrList, -1);

            closeSolrClient((SolrClient)client);
        }
    }


    private  void closeSolrClient(SolrClient client)
    {
        try
        {
            if (client != null) {
                client.close();
            }
        }
        catch (Exception localException) {}
    }

    public  List<Map<String, Object>> queryDocs(Map<String, String> parMap)
            throws Exception
    {
        SolrClient client = new HttpSolrClient.Builder(solrUrl).build();
        SolrQuery params = new SolrQuery();
        Iterator<Map.Entry<String, String>> it = parMap.entrySet().iterator();
        while (it.hasNext())
        {
            Map.Entry<String, String> entry = (Map.Entry)it.next();
            params.set((String)entry.getKey(), new String[] { (String)entry.getValue() });
        }
        QueryResponse rsp = client.query(solrCore, params);
        SolrDocumentList docs = rsp.getResults();
        List<Map<String, Object>> result = new ArrayList();
        for (SolrDocument doc : docs)
        {
            Collection<String> coll = doc.getFieldNames();
            Map<String, Object> map = new HashMap();
            for (String String : coll) {
                map.put(String, doc.getFieldValue(String));
            }
            result.add(map);
        }
        closeSolrClient(client);
        return result;
    }

}
